package com.hualidianping.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.github.benmanes.caffeine.cache.Cache;
import com.hualidianping.dto.Result;
import com.hualidianping.entity.ShopType;
import com.hualidianping.mapper.ShopTypeMapper;
import com.hualidianping.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import springfox.documentation.annotations.Cacheable;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.hualidianping.utils.RedisConstants.CACHE_SHOP_TYPE_LIST_KEY;
import static com.hualidianping.utils.RedisConstants.CACHE_SHOP_TYPE_LIST_TTL;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Slf4j
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    private StringRedisTemplate stringRedisTemplate;
    //声明Caffeine缓存
//    private final Cache<String,ShopType> cache;
    @Autowired
    public ShopTypeServiceImpl(StringRedisTemplate stringRedisTemplate){
        this.stringRedisTemplate=stringRedisTemplate;
//        this.cache = (Cache<String,ShopType>) cacheManager.getCache(CACHE_SHOP_TYPE_LIST_KEY);
    }
    /**
     * 将不常变的商铺类型列表缓存到redis中
     * @return
     */
    @Override

    public Result queryShopTypeList() {
        //从缓存中查找

        //1.从redis查询商铺类型列表缓存
        String key=CACHE_SHOP_TYPE_LIST_KEY;
        List<String> shopTypeJsonList = stringRedisTemplate.opsForList().range(key,
                0, -1);
        //2.
        if(shopTypeJsonList!=null&& CollectionUtil.isNotEmpty(shopTypeJsonList)){
            // 3.缓存命中，直接返回
            log.debug("从redis查询到商铺类型列表:{}", shopTypeJsonList);
            List<ShopType> shopTypeList=shopTypeJsonList.stream().map(shopTypeJson->
                            JSONUtil.toBean(shopTypeJson,ShopType.class))
                    .collect(Collectors.toList());
            return Result.ok(shopTypeList);
        }
        //4.缓存未命中，查询数据库
        List<ShopType> shopTypeList = query().orderByAsc("sort").list();
        //5.数据库记录存在，写入redis
        if(CollectionUtil.isNotEmpty(shopTypeList)){
            log.debug("从数据库查询到商铺类型列表:{}", shopTypeList);
            shopTypeJsonList=shopTypeList.stream()
                    .map(JSONUtil::toJsonStr)
                    .collect(Collectors.toList());
            stringRedisTemplate.opsForList().rightPushAll(key,
                    shopTypeJsonList);
            stringRedisTemplate.expire(key,CACHE_SHOP_TYPE_LIST_TTL, TimeUnit.MINUTES);
        }else {
            return Result.fail("商铺类型列表为空！");
        }
        //6.返回
        return Result.ok(shopTypeList);
    }
}
